{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1.知识图谱\n",
   "id": "f6d6e9fea2619869"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-15T13:38:43.489461Z",
     "start_time": "2025-04-15T13:38:41.492925Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain.memory import ConversationKGMemory\n",
    "from langchain_ollama import ChatOllama\n",
    "llm = ChatOllama(base_url=\"http://10.2.4.31:11434\", model=\"qwen2.5\")\n",
    "# llm.invoke(\"你好\")\n",
    "memory = ConversationKGMemory(llm=llm)\n",
    "memory.save_context({\"input\": \"帮我找一下tomie\"}, {\"output\": \"对不起请问什么是tomie？\"})\n",
    "memory.save_context({\"input\": \"tomie是一个培训讲师\"}, {\"output\": \"好的，我知道了\"})"
   ],
   "id": "b58b5f5378e6333d",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-15T13:38:46.912846Z",
     "start_time": "2025-04-15T13:38:45.875504Z"
    }
   },
   "cell_type": "code",
   "source": "memory.load_memory_variables({\"input\": \"tomie是谁？\"})",
   "id": "a172bc1a40f67c1d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': 'On Tomie: Tomie is a training instructor.'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-15T13:39:23.453694Z",
     "start_time": "2025-04-15T13:39:23.162790Z"
    }
   },
   "cell_type": "code",
   "source": "memory.get_current_entities(\"tomie最喜欢做什么事情？\") # 提取实体概念",
   "id": "839408ebc0e65ac2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Tomie']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-15T13:40:13.370296Z",
     "start_time": "2025-04-15T13:40:11.750763Z"
    }
   },
   "cell_type": "code",
   "source": "memory.get_knowledge_triplets(\"tomie最喜欢打游戏\") # 获取知识图谱三元知识组：主题、动作、作用对象",
   "id": "22019cb3ecdc1eb1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[KnowledgeTriple(subject='Tomie', predicate='likes to play', object_='games')]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2.长对话在内存中的处理\n",
   "id": "67db1e06f5d70785"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-15T13:44:00.548858Z",
     "start_time": "2025-04-15T13:44:00.542770Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "a1892b0e16809ac9",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/wk/vfsd_0ld2zdd4k_k9zlb6vrh0000gn/T/ipykernel_69643/2580935732.py:2: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
      "  memory = ConversationSummaryMemory(llm=llm)\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "15f3f2e326ab1ed8"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain",
   "language": "python",
   "name": "langchain"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
